package com.baidu.fenfayun.demo

import com.baidu.fenfayun.demo.model.Todo
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import io.realm.Realm
import io.realm.RealmResults
import org.jetbrains.anko.*
import org.jetbrains.anko.support.v4.UI
import org.jetbrains.anko.support.v4.find
import java.util.*
import org.jetbrains.anko.sdk21.coroutines.onClick
import com.baidu.yun.gameline.R


class EditFragment : Fragment() {

   val TODO_ID_KEY: String = "todo_id_key"

   val realm: Realm = Realm.getDefaultInstance()

   var todo: Todo? = null
   var todoshow:TextView? = null

   companion object {
      fun newInstance(id: String): EditFragment {
         var args: Bundle = Bundle()
         args.putString("todo_id_key", id)
         var editFragment: EditFragment = newInstance()
         editFragment.arguments = args
         return editFragment
      }

      fun newInstance(): EditFragment {
         return EditFragment()
      }
   }

   override fun onActivityCreated(savedInstanceState: Bundle?) {
      super.onActivityCreated(savedInstanceState)

      if(arguments != null && arguments.containsKey(TODO_ID_KEY)) {
         val todoId = arguments.getString(TODO_ID_KEY)
         todo = realm.where(Todo::class.java).equalTo("id", todoId).findFirst()
         val todoTitle = find<EditText>(R.id.todo_title)
         todoTitle.setText(todo?.title)
         val todoDesc = find<EditText>(R.id.todo_desc)
         todoDesc.setText(todo?.description)
         val add = find<Button>(R.id.todo_add)
         add.setText(R.string.save)
      }
   }

   override fun onDestroy() {
      super.onDestroy()
      realm.close()
   }

   /**
    *  A private function to create a TODO item in the database (Realm).
    *
    *  @param title the title edit text.
    *  @param desc the description edit text.
    */
    private fun createTodoFrom(title: EditText, desc: EditText) {
       realm.beginTransaction()
       if(title?.text.isNullOrBlank()){
          activity.toast("请输入")
       }
       // Either update the edited object or create a new one.
       var t = todo?: realm.createObject(Todo::class.java,UUID.randomUUID().toString())
       t.title = title.text.toString()
       t.description = desc.text.toString()
       realm.commitTransaction()
       //显示todolist
       val todos:RealmResults<Todo> = realm.where(Todo::class.java).findAll()
       /*val todoshow = find<TextView>(R.id.todo_show)*/
       var todolist = ""
       for(one in todos){
          todolist += one.title+"\n";
       }
       todoshow?.setText(todolist)
       // Go back to previous activity
       activity.supportFragmentManager.popBackStack();
    }

    fun clearAll(){
       realm.beginTransaction()
       realm.delete(Todo::class.java)
       realm.commitTransaction()
       todoshow?.setText(" ")

    }
  
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
       return UI {
          verticalLayout {
             padding = dip(30)
             var title = editText {
                id = R.id.todo_title
                hintResource = R.string.title_hint
             }

             var desc = editText {
                id = R.id.todo_desc
                hintResource = R.string.description_hint
             }
             button {
                id = R.id.todo_add
                textResource = R.string.add_todo
                onClick { view -> createTodoFrom(title, desc) }
             }
             button("removeAll") {
                onClick { 
                   clearAll() 
                }
             }

             todoshow = textView()
          }
       }.view
    }
 }
